import waves from "@/directive/waves"; // Waves directive
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
// import { alertMessage, alertWithCallback, isValidateNull } from "@/utils/utils";
import { mapGetters } from 'vuex';
import { getCompanyItems, getAreaList, getAlertTypeListApi, getAlertReportData } from "@/views/statistic-mange/statistic-mange.service";
import { dateFormat } from "@/utils/date-helper";

export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      file_url: process.env.VUE_APP_FILE_URL, // 头像url
      recordNum: 20, // 分页组件默认的页数, 分页里的select默认10, 20, 50
      totalNum: 0, // 数据总数
      pageNum: 1, // 页数
      data: [], // 用户列表
      searchCriteria: {}, // 搜索条件
      showSelectCarDetailDialog: false, // 车辆详情
      reportType: '0',
      reportTime: dateFormat(new Date(), 'yyyy-MM-dd'), // 当前统计时间
      companies: [], // 运输单位
      areaList: [], // 行政区
      alertType: [] // 报警类型
    }
  },
  computed: {
    ...mapGetters([
      'codesCheck'
    ])
  },
  created() {
    this.getCompanyList();
    this.getAreas();
    this.getAlertTypeList();
    this.getDataList();
  },
  methods: {
    // 设置table index
    setIndex(index) {
      return (this.pageNum - 1) * this.recordNum + index + 1;
    },

    // 获取行政区
    getAreas() {
      getAreaList().then(res => {
        this.areaList = res.data
      })
    },

    // 获取报警类型
    getAlertTypeList() {
      let params = {
        pageNum: 1,
        recordNum: 10,
        queryStr: 'ott_alarm_info.enumAlarmType'
      }
      getAlertTypeListApi(params).then(res => {
        this.alertType = res.data
      })
    },

    // 切换报表类型
    changeReport(type) {
      this.reportType = type;
      switch (type) {
        case '0': this.reportTime = dateFormat(new Date(), 'yyyy-MM-dd'); break;
        case '1': this.reportTime = dateFormat(new Date(), 'yyyy-MM'); break;
        case '2': this.reportTime = dateFormat(new Date(), 'yyyy'); break;
      }
    },

    // 获取列表
    getDataList() {
      this.searchCriteria.pageNum = this.pageNum;
      this.searchCriteria.recordNum = this.recordNum;
      this.searchCriteria.reportType = this.reportType;
      this.searchCriteria.startTime = this.reportTime;
      getAlertReportData(this.searchCriteria).then(res => {
        this.totalNum = res.dataCount;
        this.data = res.data;
      });
    },

    // 根据关键字进行搜索
    search() {
      this.pageNum = 1;
      this.getDataList();
    },

    // 重置搜索
    reset() {
      this.pageNum = 1;
      this.searchCriteria = {};
      this.getDataList();
    },

    // 运输单位列表
    getCompanyList() {
      getCompanyItems().then(res => {
        this.companies = res.data;
      });
    },

    // 导出数据
    exportData(event) {
      this.handleDownload();
    },

    // 导出数据
    handleDownload() {
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['排序', 'Serial No.', '运输状态', '报警时间', '车辆名称', '车牌号码', '车辆位置', '压力数据', '排放次数', '排放累计时长', '人孔盖状态', '泄压阀状态', '排放报警', '设备断电报警', '油罐气密性报警', '油气超压报警', '解除状态', '载重量', '介质类型', '运输单位', '联系人', '联系方式'];
        const filterVal = ['index', 'equipNo.dtuNo', 'transportStatus', 'createTime', 'vehicleName', 'licensePlate', 'position', 'pressure', 'equipNo.disCount', 'equipNo.disTimeAll', 'equipNo.isEntrance', 'equipNo.isPreValve', 'equipNo.isWarningDis', 'equipNo.isPowerFail', 'equipNo.isAirtightness', 'equipNo.isOverFail', 'remove', 'loadingCapacity', 'medium', 'compName', 'contact', 'phone'];
        // for (let key in (this.workOrders[0] ? this.workOrders[0] : {})) {
        //   tHeader.push(key);
        //   filterVal.push(key);
        // }
        const list = this.data.alarmInfoModelList;
        const data = this.formatJson(filterVal, list);
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '报警报表' + dateFormat(new Date(), 'yyyyMMddHHmmss'),
          autoWidth: true,
          bookType: 'xlsx'
        });
      })
    },

    // 格式化导出数据
    formatJson(filterVal, jsonData) {
      return jsonData.map((v, index) => filterVal.map((j) => {
        if (j == 'pressure') {
          const tempArr = [];
          if (v.equipNo.pressure.pa01 != '0') {
            tempArr.push('1#油气压力 ' + v.equipNo.pressure.pa01 + ' Pa');
          }
          if (v.equipNo.pressure.pa02 != '0') {
            tempArr.push('2#油气压力 ' + v.equipNo.pressure.pa02 + ' Pa');
          }
          if (v.equipNo.pressure.pa03 != '0') {
            tempArr.push('3#油气压力 ' + v.equipNo.pressure.pa03 + ' Pa');
          }
          return tempArr.join(', ');
        } else if (j == 'index') {
          return index + 1;
        } if (j.indexOf('.') != -1) {
          const temp = j.split('.');
          if (j == 'equipNo.isEntrance' || j == 'equipNo.isPreValve') {
            return v[temp[0]][temp[1]] == '1' ? '打开' : '关闭'
          }
          if (j == 'equipNo.isWarningDis' || j == 'equipNo.isPowerFail' || j == 'equipNo.isAirtightness' || j == 'equipNo.isOverFail') {
            return v[temp[0]][temp[1]] == '1' ? '报警' : '无报警'
          }
          return v[temp[0]][temp[1]];
        } else if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }
  }
}
